package com.example.lab2;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import javax.security.sasl.SaslServer;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@EnableConfigurationProperties(CustomProperties.class)
public class L2AutoConfig {

    @Bean
    @ConditionalOnProperty(prefix = "sai.auto",name = "enable",havingValue = "true")
    CommandLineRunner createCustomCommandLineRunner(Environment environment)
    {
        return new CustomCommandLineRunner(environment);
    }
/**   自动生成线程池 */
   @Bean(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME)
    ApplicationEventMulticaster customApplicationEventMulticaster(ThreadPoolTaskExecutor taskExecutor) {
        SimpleApplicationEventMulticaster eventMulticaster = new SimpleApplicationEventMulticaster();
        eventMulticaster.setTaskExecutor(taskExecutor);
        return eventMulticaster;
    }

/**    手动生成线程池*/
/*
    @Bean(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME)
    ApplicationEventMulticaster customApplicationEventMulticaster(Executor taskExecutor) {
        SimpleApplicationEventMulticaster eventMulticaster = new SimpleApplicationEventMulticaster();
        eventMulticaster.setTaskExecutor(taskExecutor);
        return eventMulticaster;
    }
/**
        corePoolSize： 线程池核心线程数
        maximumPoolSize：线程池最大数
        keepAliveTime： 空闲线程存活时间
        unit： 时间单位
        workQueue： 线程池所使用的缓冲队列
        threadFactory：线程池创建线程使用的工厂
        handler： 线程池对拒绝任务的处理策略
 */
/*
    @Bean(AsyncAnnotationBeanPostProcessor.DEFAULT_TASK_EXECUTOR_BEAN_NAME)
    Executor customThreadPoolTaskExecutor() {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                2, 4, 60,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(100));
        return threadPoolExecutor;
    }

*/

}
